Ext.override(Ext.grid.plugin.CellEditing,{  
    onSpecialKey: function(ed, field, e) {  
        var grid = this.grid,sm;  
        if (e.getKey() === e.TAB) {  
            e.stopEvent();  
            sm = grid.getSelectionModel();  
            if (sm.onEditorTab)sm.onEditorTab(this, e);  
        }else if(e.getKey() === e.ENTER){  
            e.stopEvent();  
            sm = grid.getSelectionModel();  
            if (sm.onEditorEnter)sm.onEditorEnter(this, e);  
        }  
    }  
});
Ext.override(Ext.selection.RowModel, {
    lastId:null,  
    onEditorTab: function(ep, e) {  
        var me = this,  
            view = me.view,  
            record = ep.getActiveRecord(),  
            header = ep.getActiveColumn(),  
            position = view.getPosition(record, header),  
            direction = e.shiftKey ? 'left' : 'right',  
            newPosition = view.walkCells(position, direction, e, false),  
            newId=newPosition.row,  
            grid=view.up('gridpanel');  
  
        if (me.lastId!=newId && me.lastId!=null){  
            deltaX = me.lastId<newId? -Infinity : Infinity;  
            header=grid.headerCt.getHeaderAtIndex(newPosition.column);  
            if(header){  
                while(!header.getEditor()){  
                    newPosition= view.walkCells(newPosition,direction, e, false);  
                    header=grid.headerCt.getHeaderAtIndex(newPosition.column);  
                }  
            }  
        }else{  
            deltaX = ep.context.column.width * (direction== 'right' ? 1 : -1);  
        }  
        grid.scrollByDeltaX(deltaX);  
        me.lastId=newPosition.row;  
        Ext.defer(function(){  
            if (newPosition)ep.startEditByPosition(newPosition);  
            else ep.completeEdit();  
        }, 100);  
    },  
    onEditorEnter:function(ep,e){  
        var me = this,  
            view = me.view,  
            record = ep.getActiveRecord(),  
            header = ep.getActiveColumn(),  
            position = view.getPosition(record, header),  
            direction = e.shiftKey ? 'up' : 'down',  
            newPosition = view.walkCells(position, direction, e, false),  
            newId=newPosition.row,  
            grid=view.up('gridpanel');  
  
        deltaY=20 * (direction== 'down' ? 1 : -1);  
        grid.scrollByDeltaY(deltaY);  
        me.lastId=newPosition.row;  
        Ext.defer(function(){  
            if (newPosition)ep.startEditByPosition(newPosition);  
            else ep.completeEdit();  
        }, 100);  
    }  
}); 
var cellEdit = Ext.create('Ext.grid.plugin.CellEditing', {
    clicksToEdit: 1
});
var ScanIndex = 0;
Ext.define('HIT.view.ExpressScanGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.expressscangrid',

    cls: 'custom-grid',
    frame: false,
    width: 1200,
    columnLines: false,
     store: 'DealScan',

    initComponent: function() {
        var me = this;          
        
        Ext.applyIf(me, {
            columns: [
                {
                    xtype: 'rownumberer',
                    resizable: true,
                    width: 30
                },                
                {
                    xtype: 'gridcolumn',
                    width: 120,
                    align: 'center',
                    dataIndex: 'document_no',
                    text: Share.T('单据号')
                },
                {
                    xtype: 'gridcolumn',
                    width: 180,
                    align: 'left',
                    dataIndex: 'cust_name_chn',
                    style:'text-align:center;',
                    text: Share.T('客户名称')
                },
                {
                    xtype: 'gridcolumn',
                    width: 120,
                    align: 'center',
                    dataIndex: 'invoice_no',
                    text: Share.T('发票号码')
                },
                {
                    xtype: 'gridcolumn',
                    renderer: Ext.util.Format.numberRenderer('000,000,000.00'),
                    width: 120,
                    align: 'right',
                    dataIndex: 'invoice_amount',
                    style:'text-align:center;',
                    text: Share.T('发票金额')
                },
                {
                    xtype: 'gridcolumn',
                    width: 120,
                    align: 'left',
                    dataIndex: 'department',
                    style:'text-align:center;',
                    text: Share.T('部门')
                },
                {
                    xtype: 'gridcolumn',
                    width: 100,
                    align: 'center',
                    format : 'Y-m-d',
                    dataIndex: 'invoice_date',
                    text: Share.T('发票日期')
                },
                {
                    xtype: 'gridcolumn',
                    width: 100,
                    align: 'left',
                    dataIndex: 'express_name',
                    style:'text-align:center;',
                    text: Share.T('快递公司')
                },
                {
                    xtype: 'gridcolumn',
                    width: 120,
                    align: 'left',
                    style:'text-align:center;',
                    dataIndex: 'express_no',
                    text: Share.T('快递单号'),
                    editor:{
                    	xtype: 'textfield',
                    	fieldStyle:'background:#FFFF80;',
                    	listeners:{
                    		focus:function(){
                    			//获取当前扫描行，并暂存到变量ScanIndex
                           		var grid = Ext.getCmp('tab-deal-scan');
                       			var recordtoedit = grid.getSelectionModel().getSelection()[0];
                       			ScanIndex = grid.getStore().indexOf(recordtoedit);
                       			this.focus(true,true);
                    		},
                    		specialkey : function(compo, e) {
                    			//当输入 enter 键时，保存当前快递单号，并将光标移向下一行
                    			if (e.getKey() == Ext.EventObject.ENTER) {
                    				if(compo.value == null){
                    					return;
                    				}
//                    				var typeSel = Ext.getCmp('scan-express-type');
//                    				var type = typeSel.getValue();
//                    				var name = typeSel.rawValue;
                    				var grid = Ext.getCmp('tab-deal-scan');
                    				var model = grid.getStore().getAt(ScanIndex);
//                    				model.set('express_type',type);
//                    				model.set('express_name',name);
                    				model.set('delivery_person',Ext.state.Manager.get('Cxjava_account'));
                    				//保存快递单号
                    				Share.AjaxRequest({
       	             					url : ctx + '/saveExpressNo',
       	             					params : {
       	             						id : model.get('id_head_id'),
       	             						no :compo.value
       	             					},
       	             					callback : function(json) {
       	             						if (json.resultcode != '0') {
       	             							Share.ShowMessage("I0023");
       	             							model.set('express_no','');
       	             							cellEdit.startEditByPosition({row: (ScanIndex), column: 9});
       	             						} else {
       	                  		                cellEdit.startEditByPosition({row: (ScanIndex+1), column: 9});
       	                  		            }
       	             					},
       	             					falseFun : function(json) {Share.ShowMessage("I0023");}
                    				});
                    			}
                    		},
                    		//保存当前行的快递单
                    		blur : function(compo,the, e) {
                    			if(compo.value == null){
                    				return;
                    			}
//                    			var typeSel = Ext.getCmp('scan-express-type');
//                    			var type = typeSel.getValue();
//                    			var name = typeSel.rawValue;
                    			var grid = Ext.getCmp('tab-deal-scan');
                    			var model = grid.getStore().getAt(ScanIndex);
//                    			model.set('express_type',type);
//                    			model.set('express_name',name);
                    			model.set('delivery_person',Ext.state.Manager.get('Cxjava_account'));
                    			Share.AjaxRequest({//保存快递单号
                    				url : ctx + '/saveExpressNo', 
                    				params : {
                    					id : model.get('id_head_id'),
//                    					type :type,
             						    no :compo.value
                    				},
 	             					callback : function(json) {
 	             						if (json.resultcode != '0') {               							               						
 	             							Share.ShowMessage("I0023");
 	             							model.set('express_no','');
 	             							cellEdit.startEditByPosition({row: (ScanIndex), column: 9});
 	             						}
 	             					},
 	             					falseFun : function(json) {Share.ShowMessage("I0023");}
 	             				});  
                    		}  
                    	}  
                    }
                }, {
                	xtype: 'gridcolumn',
                    width: 100,
                    align: 'center',
                    dataIndex: 'delivery_person',
                    text: Share.T('扫描人')
                }
            ],
            viewConfig: {
                enableTextSelection:true
            },
            selModel: Ext.create('Ext.selection.CheckboxModel', {
            	checkOnly: false
            }), 
            selType: 'cellmodel',
            plugins:[cellEdit],
            dockedItems: [
				{
				    xtype: 'toolbar',
				    dock: 'top',
				    items: [
				     {
				          xtype: 'tbspacer',
				          width: 20
				     },
//				     {
//				        	xtype : 'combobox',
//							editable : false,
//							labelWidth:60,
//							fieldLabel : Share.T('快递公司'),
//							id : 'scan-express-type',
//							valueField : 'value',
//							displayField : 'text',
//							store : new Ext.data.JsonStore({
// 								proxy: { 
// 									type: 'ajax', 
// 									url : ctx + '/getExpressType',
// 									params:{        	                	
// 									},
// 									reader: { 
// 										type: 'json', 
// 										totalProperty: '', 
// 										root: '' 
// 									} 
// 								}, 
// 							fields:['text','value'], 
//            	            autoLoad:true 
//				          }),
//				          value:1
//				      },
				                            
                      {
				        	xtype : 'combobox',
							editable : false,
							fieldLabel : Share.T('快递公司'),
							labelWidth:60,
					 		id : 'scan-search-type',
							valueField : 'value',
							displayField : 'text',
							store : new Ext.data.JsonStore({
								proxy: { 
									type: 'ajax', 
									url : ctx + '/getExpressType1',
									params:{        	                	
									},
									reader: { 
										type: 'json', 
										totalProperty: '', 
										root: '' 
									} 
								}, 
							fields:['text','value'], 
          	                autoLoad:true 
				          }),
				          value:-1
				          ,listeners:{
				        	  select:function(combo,records,e){
				        		  var type = combo.value;
				        		  var tabpanelId = 'tab-deal-scan';
				        	      var tab = Ext.getCmp(tabpanelId);
				        		  var store = tab.getStore();
				           	      store.currentPage = 1;
				           	      store.removeAll();
				                  store.on('beforeload', function(store) {
				          			  Ext.apply(store.proxy.extraParams , {startno:null
				          				                                 ,endno:null
				          				                                 ,nos:null
				          				                                 ,sexpressno:null
				          				                                 ,eexpressno:null
				          				                                 ,startday:null
				          				                                 ,endday:null
				          				                                 ,custom:null
				          				                                 ,type:(type==-1?null:type)
				          				                                 });
				                });
				               store.load(); 
				        	  }
				          }
				      },
				      
				     {
				        xtype: 'tbspacer',
				        width: 20
				     },
				     {
				       xtype: 'tbseparator'
				     }
				   ]
				 },
                {
                    xtype: 'pagingtoolbar',
                    dock: 'bottom',
                    inputItemWidth:45,
                    width: 360,
                    displayInfo: true,
                    store: 'DealScan',
                    plugins: [{
                        ptype: "pagesize",
                        uxPageSize: page_size}]
                }
            ]
        });       
        
        me.callParent(arguments);
    }
});